package com.liuyk.commonlib.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.utility.DeviceUtils;

/**
 * Dialog基类
 * <p>
 * Created by liuyakui on 2020/5/15.
 */
public abstract class BaseDialog extends Dialog implements View.OnClickListener {
    private NeutralClickListener neutralClickListener;
    private PositiveClickListener positiveClickListener;
    private CenterClickListener centerClickListener;

    private int mHeight;
    private int mWidth;
    private float mDimAmount = -1;

    private int mAnimationResId = -1;
    private View parentView;
    private Window mWindow;

    private TextView mLeftText;
    private TextView mRightText;
    private TextView mContentText;
    private TextView mTitleText;

    private boolean mDismiss = true;

    private int gravity = -1;

    public BaseDialog(@NonNull Context context) {
        this(context, 0);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        parentView = LayoutInflater.from(getContext()).inflate(contentView(), null);
        mLeftText = parentView.findViewById(R.id.left_btn);
        if (mLeftText != null) {
            mLeftText.setOnClickListener(this);
        }
        mRightText = parentView.findViewById(R.id.right_btn);
        if (mRightText != null) {
            mRightText.setOnClickListener(this);
        }
        mContentText = parentView.findViewById(R.id.content);
        mTitleText = parentView.findViewById(R.id.title);
        setContentView(parentView);
        setCancelable(false);
        mWindow = getWindow();
    }

    public final <T extends View> T findViewById(int resId) {
        return parentView.findViewById(resId);
    }

    @Override
    public void show() {
        super.show();
        //大小需在show之后设置才有效，并且在wrap_content的时候，跟布局的宽高无效
        if (mWindow != null) {
            if (mAnimationResId != -1) {
                mWindow.setWindowAnimations(mAnimationResId);
            }
            mWindow.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams attributes = mWindow.getAttributes();
            attributes.gravity = gravity != -1 ? gravity : Gravity.CENTER;
            if (mWidth != 0) {
                attributes.width = mWidth;
            } else {
                attributes.width = (int) (DeviceUtils.getWindowWidth(getContext()) * 0.8);
            }
            if (mHeight != 0) {
                attributes.height = mHeight;
            } else {
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            }
            if (mDimAmount != -1) {
                attributes.dimAmount = mDimAmount;
            }
            mWindow.setAttributes(attributes);
        }
    }

    protected abstract int contentView();

    public void setHeightParams(int mHeight) {
        this.mHeight = mHeight;
    }

    public void setWidthParams(int mWidth) {
        this.mWidth = mWidth;
    }

    public void setAnimationRes(int animationRes) {
        mAnimationResId = animationRes;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.left_btn) {
            if (mDismiss) {
                dismiss();
            }
            if (neutralClickListener != null) {
                neutralClickListener.neutral(v);
            }
        } else if (id == R.id.right_btn) {
            if (mDismiss) {
                dismiss();
            }
            if (positiveClickListener != null) {
                positiveClickListener.positive(v);
            }
        } else if (id == R.id.center) {
            if (mDismiss) {
                dismiss();
            }
            if (centerClickListener != null) {
                centerClickListener.centerClick(v);
            }
        }

    }

    @Override
    public void dismiss() {
        //not attached to window manager
        try {
            super.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setNeutralClickListener(NeutralClickListener neutralClickListener) {
        this.neutralClickListener = neutralClickListener;
    }

    public void setPositiveClickListener(PositiveClickListener positiveClickListener) {
        this.positiveClickListener = positiveClickListener;
    }

    public void setCenterClickListener(CenterClickListener centerClickListener) {
        this.centerClickListener = centerClickListener;
    }

    public void setNeutralBackground(Drawable drawable) {
        if (mLeftText != null && drawable != null) {
            mLeftText.setBackground(drawable);
        }
    }

    public void setNeutralText(String text) {
        if (mLeftText == null) {
            return;
        }

        if (TextUtils.isEmpty(text)) {
            mLeftText.setVisibility(View.GONE);
            return;
        }

        mLeftText.setText(text);
        mLeftText.setVisibility(View.VISIBLE);
    }

    public void setPositiveText(String text) {
        if (mRightText == null) {
            return;
        }

        if (TextUtils.isEmpty(text)) {
            mRightText.setVisibility(View.GONE);
            return;
        }

        mRightText.setText(text);
        mRightText.setVisibility(View.VISIBLE);
    }

    public void setDimAmount(float mDimAmount) {
        this.mDimAmount = mDimAmount;
    }

    public void setContentText(String text) {
        if (mContentText != null) {
            if (!TextUtils.isEmpty(text)) {
                mContentText.setText(text);
                mContentText.setVisibility(View.VISIBLE);
            } else {
                mContentText.setVisibility(View.GONE);
            }
        }
    }

    public void setContentGravity(int position) {
        if (mContentText != null) {
            mContentText.setGravity(position);
        }
    }

    public void setTitleText(String text) {
        if (mTitleText != null) {
            if (!TextUtils.isEmpty(text)) {
                mTitleText.setText(text);
                mTitleText.setVisibility(View.VISIBLE);

            } else {
                mTitleText.setVisibility(View.GONE);
                //没有标题 内容字号修改成16
                if (mContentText != null) {
                    mContentText.setTextSize(16f);
                }
            }
        }
    }

    public void setContentTextColor(int color) {
        if (mContentText != null) {
            mContentText.setTextColor(color);
        }
    }

    public TextView getLeftText() {
        return mLeftText;
    }

    public TextView getRightText() {
        return mRightText;
    }

    public TextView getContentText() {
        return mContentText;
    }

    public TextView getTitleText() {
        return mTitleText;
    }

    public void setDismiss(boolean mDismiss) {
        this.mDismiss = mDismiss;
    }

    public interface NeutralClickListener {
        void neutral(View view);
    }

    public interface PositiveClickListener {
        void positive(View view);
    }

    public interface CenterClickListener {
        void centerClick(View view);
    }
}
